package agenda;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Iterator;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Wesley Brenno, Célio Roberto, Ricardo Magno
 */
public class JanelaContato extends javax.swing.JFrame {
	
    /** Creates new form JanelaCadastroDeContatos */
    public JanelaContato(Contato Contato) {
        this.Contato = Contato;
        initComponents();
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setIconImage((new ImageIcon(getClass().getResource("/agenda/logo.png")).getImage()));
        setTitle("Contato");
        this.setLocationRelativeTo(null);  
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabelNome = new javax.swing.JLabel();
        jLabelCadastroDeContatos = new javax.swing.JLabel();
        jLabelEndereco = new javax.swing.JLabel();
        jLabelData = new javax.swing.JLabel();
        jLabelCelular = new javax.swing.JLabel();
        jLabelDescricao = new javax.swing.JLabel();
        jTextAreaDescricao = new javax.swing.JTextArea(Contato.getDescricao());
        jTextFieldEndereco = new javax.swing.JTextField(Contato.getEndereco());
        jTextFieldCelular = new javax.swing.JTextField((String)Contato.getFormasDeContato().get("Celular"));
        jTextFieldNome = new javax.swing.JTextField(Contato.getNome());
        jLabelFoto = new javax.swing.JLabel();
        jButtonSalvar = new javax.swing.JButton();
        jButtonAddFormasDeContato = new javax.swing.JButton();
        jButtonAlterarFoto = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField(Contato.getDataDeAniversario());
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableFormasDeContato = new javax.swing.JTable();
        jLabelDescricao1 = new javax.swing.JLabel();
        jButtonExcluiContato = new javax.swing.JButton();
        jButtonRemoverForma = new javax.swing.JButton();
        jButtonEventos = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelNome.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabelNome.setText("Nome:");

        jLabelCadastroDeContatos.setFont(new java.awt.Font("Monotype Corsiva", 3, 24)); // NOI18N
        jLabelCadastroDeContatos.setText("Dados do Contato");

        jLabelEndereco.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabelEndereco.setText("Endereço:");

        jLabelData.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabelData.setText("Data de nascimento:");

        jLabelCelular.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabelCelular.setText("Celular:");

        jLabelDescricao.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabelDescricao.setText("Descrição:");

        jLabelFoto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/agenda/sem_foto.gif"))); // NOI18N

        jButtonSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/agenda/ip_icon_02_Ok.png"))); // NOI18N
        jButtonSalvar.setText("Salvar");
        jButtonSalvar.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				int indice = Agenda.Contatos.indexOf(Contato);
				Contato.setNome(jTextFieldNome.getText());Contato.setEndereco(jTextFieldEndereco.getText());Contato.setDataDeAniversario(jTextField1.getText());Contato.CadastraFormaDeContato("Celular", jTextFieldCelular.getText());Contato.setFoto(jLabelFoto.getIcon());Contato.setDescricao(jTextAreaDescricao.getText());
				for(int i=0;i < jTableFormasDeContato.getModel().getRowCount();i++){
					Contato.CadastraFormaDeContato((String)(jTableFormasDeContato.getValueAt(i,1)),(String)(jTableFormasDeContato.getValueAt(i,1)));
				}
				Agenda.Contatos.set(indice,Contato);
				try {
					Arquivos.salvarObjeto(new File("contatos.dat"), Agenda.Contatos);
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				setVisible(false);
				dispose();
			} 	
        });

        jButtonAddFormasDeContato.setText("Nova forma de Contato");
        jButtonAddFormasDeContato.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				final JFrame FormaContato = new JFrame("Forma de Contato");
				JPanel Panel = new JPanel();
				FormaContato.setSize(250,170);
				JLabel Forma = new JLabel("Forma de Contato");
				final JTextField Forma1 = new JTextField(20);
				JLabel Endereco = new JLabel("Endereço");
				final JTextField Endereco1 = new JTextField(20);
				JButton Ok = new JButton("Ok");
				Ok.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent arg0) {
						DefaultTableModel modelo = (DefaultTableModel) jTableFormasDeContato.getModel();
						modelo.addRow(new String[]{Forma1.getText(),Endereco1.getText()});
						FormaContato.setVisible(false);
						FormaContato.dispose();
					}
				});
				Panel.add(Forma);Panel.add(Forma1);Panel.add(Endereco);Panel.add(Endereco1);Panel.add(Ok);
				FormaContato.add(Panel);
				FormaContato.setVisible(true);
			}
        	
        });

        jButtonAlterarFoto.setText("Alterar foto");
        jButtonAlterarFoto.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser jFileChooser1 = new javax.swing.JFileChooser();
				jFileChooser1.setFileFilter(new FileNameExtensionFilter("JPG","jpg"));
				jFileChooser1.showOpenDialog(null);
				File file = jFileChooser1.getSelectedFile();
				RedimensionarImagem.redimensionaImagem(file,jTextFieldNome.getText()+"."+file.getName().substring(file.getName().lastIndexOf(".")+1,file.getName().length()),121,164);
				Icon icon = new ImageIcon((jTextFieldNome.getText()+"."+file.getName().substring(file.getName().lastIndexOf(".")+1,file.getName().length())));
				jLabelFoto.setIcon(icon);
				
			}
        	
        });

        jButtonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/agenda/ip_icon_02_Cancel.png"))); // NOI18N
        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				dispose();
				
			}
        	
        });

        jTableFormasDeContato.setModel(new javax.swing.table.DefaultTableModel(
            new String [][] {},
            new String [] {
                "Formas de Contato", "Endereço"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        Iterator<String> it = Contato.getFormasDeContato().keySet().iterator();
		while (it.hasNext()) {
			String key = it.next();
			DefaultTableModel modelo = (DefaultTableModel) jTableFormasDeContato.getModel();
			modelo.addRow(new String[]{key,(String) Contato.getFormasDeContato().get(key)});
			}
        jScrollPane1.setViewportView(jTableFormasDeContato);

        jLabelDescricao1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabelDescricao1.setText("Outras formas de Contato:");
        
        jButtonRemoverForma.setText("Remover forma de Contato");
        jButtonRemoverForma.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				try{
					Contato.getFormasDeContato().remove(jTableFormasDeContato.getValueAt(jTableFormasDeContato.getSelectedRow(),1));
					DefaultTableModel modelo = (DefaultTableModel) jTableFormasDeContato.getModel();
					modelo.removeRow(jTableFormasDeContato.getSelectedRow());	
				}catch(Exception e2){
					final JFrame Erro = new JFrame();
					Erro.setIconImage((new ImageIcon(getClass().getResource("/agenda/logo.png")).getImage()));
				    Erro.setLocationRelativeTo(null);  
					Erro.setSize(200,100);
					JPanel Painel = new JPanel();
					JLabel Mensagem = new JLabel("Selecione algum item primeiro");
					JButton Ok = new JButton("Ok");
					Ok.addActionListener(new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent arg0) {
							Erro.setVisible(false);
							Erro.dispose();
						}
						
					});
					Painel.add(Mensagem);Painel.add(Ok);Erro.add(Painel);
					Erro.setVisible(true);
				}
				
				}	
        	
        });

        jButtonExcluiContato.setIcon(new javax.swing.ImageIcon(getClass().getResource("/agenda/remove_user.png"))); // NOI18N
        jButtonExcluiContato.setToolTipText("Excluir Contato");
        jButtonExcluiContato.addActionListener (new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				Agenda.Contatos.remove(Contato);
				setVisible(false);
				dispose();
			}
        	
        });
        jButtonEventos.setIcon(new javax.swing.ImageIcon("C:\\Users\\Brenno\\Desktop\\icon_contacts_calendar.png")); // NOI18N
        jButtonEventos.setToolTipText("Ver eventos com este contato");
        jButtonEventos.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				new JanelaEventoComContato(Contato).setVisible(true);	
			}
        });
        
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabelFoto, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButtonAlterarFoto)
                                .addGap(23, 23, 23)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(88, 88, 88)
                                .addComponent(jLabelCadastroDeContatos))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextAreaDescricao, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabelData)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabelCelular)
                                        .addGap(10, 10, 10)
                                        .addComponent(jTextFieldCelular, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabelDescricao)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                            .addComponent(jLabelEndereco)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jTextFieldEndereco))
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                            .addComponent(jLabelNome)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jTextFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, 422, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addComponent(jLabelDescricao1)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButtonExcluiContato, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonEventos, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jButtonCancelar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButtonSalvar))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jButtonRemoverForma)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonAddFormasDeContato)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jLabelCadastroDeContatos)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelNome)
                            .addComponent(jTextFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelEndereco)
                            .addComponent(jTextFieldEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabelData)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabelCelular))
                            .addComponent(jTextFieldCelular, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelDescricao)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextAreaDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(63, 63, 63)
                        .addComponent(jLabelFoto, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonAlterarFoto)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelDescricao1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonAddFormasDeContato, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonRemoverForma, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonSalvar)
                            .addComponent(jButtonCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE))
                        .addGap(33, 33, 33))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButtonExcluiContato, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                            .addComponent(jButtonEventos, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE))
                        .addContainerGap())))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }
    private javax.swing.JButton jButtonAddFormasDeContato;
    private javax.swing.JButton jButtonAlterarFoto;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonExcluiContato;
    private javax.swing.JButton jButtonSalvar;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel jLabelCadastroDeContatos;
    private javax.swing.JLabel jLabelCelular;
    private javax.swing.JLabel jLabelData;
    private javax.swing.JLabel jLabelDescricao;
    private javax.swing.JLabel jLabelDescricao1;
    private javax.swing.JLabel jLabelEndereco;
    private javax.swing.JLabel jLabelFoto;
    private javax.swing.JLabel jLabelNome;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableFormasDeContato;
    private javax.swing.JTextField jTextFieldCelular;
    private javax.swing.JTextArea jTextAreaDescricao;
    private javax.swing.JButton jButtonRemoverForma;
    private javax.swing.JTextField jTextFieldEndereco;
    private javax.swing.JTextField jTextFieldNome;
    private javax.swing.JButton jButtonEventos;
    private Contato Contato;
}
